package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.EmployeeDao;
import com.hqyj.pojo.Employee;
import com.hqyj.pojo.PageData;
import com.hqyj.service.EmployeeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public List<Employee> getAllEmployee() {
        System.out.println("获取成功");
        List<Employee> employeeList = employeeDao.queryAll();
        return employeeList;
    }

    @Override
    public int insertEmployee(Employee employee) {
        int result = employeeDao.insertOne(employee);

        //如果插入的数据大于一行就表示添加成功了
        System.out.println("添加成功");
        return result;
    }

    @Override
    public int updateEmployee(Employee employee) {

        //修改员工信息
        int result=employeeDao.updateOne(employee);
        System.out.println("修改成功");
        return result;
    }

    @Override
    public int deleteEmployee(Employee employee) {

        //删除成功
        int reslut=employeeDao.deleteByEmployee(employee);
        System.out.println("删除成功");
        return reslut;
    }

    @Override
    public PageData<Employee> getEmployeeByPage(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        //查询全部用户信息
        List<Employee> employees = employeeDao.queryAll();
        //创建pageInfo，pageInfo会计算分页相关信息
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);
        //创建我们自己的数据模型
        PageData<Employee> pageData = new PageData<>();

        pageData.setCurrentPage(pageNumber);
        pageData.setPageSize(pageSize);

        //填充总页数数据
        pageData.setTotalPage(pageInfo.getPages());

        //填充总记录数
        pageData.setTotalSize((int) pageInfo.getTotal());

        if (pageInfo.isHasNextPage()) {
            //有下一页，设置下一页的页码
            pageData.setNextPage(pageInfo.getNextPage());
        } else {
            //没有下一页，就是尾页
            pageData.setNextPage(pageInfo.getPages());
        }
        if (pageInfo.isHasPreviousPage()) {
            //有上一页，设置上一页的页码
            pageData.setPreviousPage(pageInfo.getPrePage());
        } else {
            //没有上一页，设置为首页
            pageData.setPreviousPage(1);
        }
        //设置每页的用户记录信息
        pageData.setList(pageInfo.getList());

        return pageData;
    }

}
